// 公共头部 uni-transition
// import myHeader from "/pages/headerfoot/pages/header.vue";
//过度动画
import uTransition from "/uni_modules/uni-transition/components/uni-transition/uni-transition.vue"
//语言
// import Lang from '/utils/lang.js';
//cache缓存
import Cache from '@/config/cache.js';
//打帮赛方法
// import hitmatchcommon from '@/common/hitmatchcommon.js';
// 基础方法
import methodBase from "/config/base.js";

export default {
	props: {
		project_make_id: String,
		project_id: String,
	},
	components: {
		// myHeader,
		uTransition,
	},
	data() {
		return {
			upishow: true, //是否问题显示、
			downishow: false, //是否答案显示、

			type: 3, // 1=字母  2=文字 3=特殊符号
			sucesswtopic: 0, //正确题数
			errorwtopic: 0, //错误题数

			ifcountdown: false, //是否显示倒计时
			seconds: 0, //秒
			lookseconds: 3, //看题秒杀
			answerseconds: 4, //答题秒杀

			items: 4, //错误题
			timer: {}, //倒计时时间
			ifpass: false, //闯关显示
			answertime: 0, //答题用时
			number: 5,
			tikuArr: [], //题库
			tiku_data: {
				tx: ["●", "★", "◆", "■", "▲", "☆", "○", "◇", "□", "△","#", "$", "%", "!", "*", "~", "￥"],
				zm: ["q", "w", "e", "r", "t", "y", "u", "i", "p", "a", "s", "d", "f", "g", "h", "j", "k", "z",
					"x", "c", "v", "b", "n", "m"
				],
				wz: ['坝', '汉', '艳', '扮', '杨', '读', '摔', '跤', '凤', '洁', '晨', '绒', '球', '服', '装', '静', '停'],
				sz: ["2", "3", "4", "5", "6", "7", "8", "9"],
			}, //题库数据
			questions: [], //出题
			answer: [], //答案

			successnumberarr: [], //问题
			my_answer: [], //我的答案
			selectedindex: 99, //选中特效数字 99没有数字
			alltime: 60, //总用时间
			loseMax: 6, //最大错题数量
			successMax: 6, //正确数量

			iffirststart: 1, //第一次进入

		}
	},
	async mounted() {
		var that = this;

		if (that.project_make_id) {
			Cache.set('project_make_id', that.project_make_id)
		}
		//console.log(that.project_id);//项目
		if (that.project_id) {
			Cache.set('project_id', that.project_id)
		}

	},
	methods: {
		//开始题
		beginsubject() {
			var that = this;
			that.iffirststart = 2; //不是第一个
			that.thequestions();
			that.ifcountdown = true;

		},
		//点击特效
		clickEffects(number) {
			var that = this;
			that.selectedindex = number
		},
		//点击事件
		clickhandle(e, number) {
			var that = this;
			that.iffirststart = 2; //不是第一个
			if (e == that.questions) { //正确
				that.sucesswtopic++;
				that.clickStyles = 'selected_sucess';
			} else { //错误
				that.errorwtopic++;
				that.clickStyles = 'selected_error';
			}
			
			if (that.sucesswtopic >= that.successMax) {
				//时间结束
				clearInterval(that.timer)
				console.log("正确提交");
			}
			
			that.successnumberarr.push(that.questions); //问题
			that.my_answer.push(e); //我的答案

			//点击特效
			that.clickEffects(number);

			setTimeout(function() {
				//显示中间过渡
				that.intermediateTransition();
				that.selectedindex = 99; //选中特效数字 99没有数字

			}, 1000); // 这里的 1000 表示延时的时间，单位是毫秒

		},
		//显示中间过渡
		intermediateTransition() {
			var that = this;
			//时间结束
			clearInterval(that.timer)
			//出题
			if (that.upishow) {
				that.questionselect();
			} else if (that.downishow) {
				that.thequestions();
			}

		},
		//倒计时
		countdown(times) {

			var that = this;
			var seconds = parseInt(times) - 1; //秒
			that.timer = setInterval(() => {
				that.seconds = seconds;
				console.log(that.answertime)
				if (that.answertime >= that.alltime) {
					//时间结束
					clearInterval(that.timer)
					console.log("时间到提交");
				}
				that.answertime++; //答题用时

				if (seconds <= 0) {
					if (that.downishow) {
						that.errorwtopic++;
					}
					console.log(that.errorwtopic)
					if (that.errorwtopic >= that.loseMax) {
						//时间结束
						clearInterval(that.timer)
						console.log("错误到提交");
					}
					//显示中间过渡
					that.intermediateTransition();


				} else {
					--seconds; //减秒数
				}
			}, 1000);
		},
		//出题
		thequestions() {
			var that = this;
			that.questions = [];
			that.downishow = false;
			that.upishow = true;
			that.seconds = that.lookseconds;
			that.countdown(that.lookseconds);
			var questionslist = [];
			if (that.type == '1') {
				questionslist = that.tiku_data['zm'];
			}else if (that.type == '2') {
				questionslist = that.tiku_data['wz'];
			}else if (that.type == '3') {
				questionslist = that.tiku_data['tx'];
			}
			that.tikuArr = questionslist;
			for (var i = 0; i < that.number; i++) {
				that.questions.push(methodBase.rand_value(questionslist));
			}
		},
		//选题出题
		questionselect() {
			var that = this;
			that.answer = [];
			that.downishow = true;
			that.upishow = false;
			that.seconds = that.answerseconds;
			that.countdown(that.answerseconds);

			that.generateErrorQuestions();
			that.dislocate(); //正确题与错误题打乱
		},
		//生成错误题
		generateErrorQuestions() {
			var that = this;
			for (var i = 0; i < that.items; i++) {
				//替换字符可变项
				that.substitutionCharacters();
			}
		},
		//替换字符可变项
		substitutionCharacters() {
			var that = this;

			var newtikuArr = JSON.parse(JSON.stringify(that.tikuArr)); //重新赋值组装数组
			var questions = JSON.parse(JSON.stringify(that.questions)); //重新赋值正常答案
			var randIndex1 = Math.round(Math.random() * (questions.length - 1)); //随机取一个数
			questions.splice(randIndex1, 1); //重新赋值正常答案

			var nowtopic = questions[randIndex1]; //查询重新赋值组装数组指定值的位置


			var oldStrIndex = newtikuArr.indexOf(nowtopic); //删除指定重新赋值组装数组的index

			newtikuArr.splice(oldStrIndex, 1); //删除已随机选中的字符串下标

			var randIndex2 = Math.round(Math.random() * (newtikuArr.length - 1)); //随机取一个数
			var nowtopicb = newtikuArr[randIndex2]; //组装数组提取一个
			// var newarr = questions.concat(nowtopicb); //组装数组
			var newquestionsc = [];
			var newquestionsa = questions.slice(0, randIndex1 + 1);
			var newquestionsb = questions.slice(randIndex1 + 1, questions.length)

			newquestionsc = newquestionsa;
			newquestionsc.push(nowtopicb);
			var len = newquestionsb.length;
			for (var i = 0; i < len; i++) {
				newquestionsc.push(newquestionsb[i]);
			}

			that.answer.push(newquestionsc); //推入错误题库
		},
		// 正确题与错误题打乱
		dislocate() {
			var that = this;
			var success = that.questions; //正确题
			var error = that.answer; //错误题
			var arr = error.concat([success]); //合并
			//打乱
			that.answer = that.shuffle(arr);
		},

		//打乱数组
		shuffle(arr) {
			var len = arr.length;
			for (var i = 0; i < len; i++) {
				var end = len - 1;
				var index = (Math.random() * (end + 1)) >> 0;
				var t = arr[end];
				arr[end] = arr[index];
				arr[index] = t;
			}
			return arr;
		},
	},
	// 在页面的 onUnload 生命周期中停止更新位置
	onUnload() {
		var that = this;
		//清除定时器
		clearInterval(that.timer);
	},
}